package model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Racket extends JPanel implements Common {

	private Point point;
	private int width;
	private int height;
	private boolean isLeft;
	private int movementSpeed;

	public Racket(int x, int y, int width, int height, boolean isLeft) {
		this.isLeft = isLeft;
		this.point = new Point(x, y);
		this.width = width;
		this.height = height;
		this.movementSpeed = height / 2;
	}

	@Override
	public int getHeight() {
		return this.height;
	}

	@Override
	public int getWidth() {
		return this.width;
	}

	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return this.point.getX();
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return this.point.getY();
	}

	public void keyPressed(KeyEvent e) {
		if (this.isLeft) {
			if (e.getKeyCode() == KeyEvent.VK_S
					&& !(this.point.getY() + this.height + this.movementSpeed > COMMON_SCREENHEIGHT))
				this.point.addY(this.movementSpeed);
			if (e.getKeyCode() == KeyEvent.VK_W
					&& !(this.point.getY() - this.movementSpeed < 0))
				this.point.addY(-this.movementSpeed);
		} else {
			if (e.getKeyCode() == KeyEvent.VK_DOWN
					&& !(this.point.getY() + this.height + this.movementSpeed > COMMON_SCREENHEIGHT)) {
				this.point.addY(this.movementSpeed);
			}
			if (e.getKeyCode() == KeyEvent.VK_UP
					&& !(this.point.getY() - this.movementSpeed < 0))
				this.point.addY(-this.movementSpeed);
		}
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(this.point.getX(), this.point.getY(), this.width,
				this.height);

	}

	public boolean isLeft() {
		return this.isLeft;
	}

	public Point getMiddle() {
		return new Point(this.width / 2 + this.point.getX(), this.height / 2
				+ this.point.getY());
	}

}
